import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc';
import { SFSchema } from '@delon/form';
import { SysUserEditComponent } from './edit/edit.component';
import { NzMessageService } from 'ng-zorro-antd';
// import { SysUserViewComponent } from './view/view.component';

@Component({
  selector: 'app-sys-user',
  templateUrl: './user.component.html',
})
export class SysUserComponent implements OnInit {
  url = `/user`;
  searchSchema: SFSchema = {
    properties: {
      name: {
        type: 'string',
        title: '姓名'
      },
      account: {
        type: 'string',
        title: '登录名'
      }
    }
  };
  @ViewChild('st') st: STComponent;
  columns: STColumn[] = [
    // { title: '行号', type: 'no' },
    { title: '编号', index: 'id' },
    { title: '姓名', index: 'name' },
    { title: '登录名', index: 'account' },
    { title: '所在部门', index: 'dept' },
    // { title: '创建时间', type: 'date', index: 'updatedAt' },
    {
      title: '',
      buttons: [
        { text: '编辑', type: 'modal', modal: { component: SysUserEditComponent }, click: 'reload' },
        {
          text: '删除', type: 'del', click: (record, modal, comp) => {
            this.http.post(`/user/delete?id=${record.id}`).subscribe((res)=>{
              this.message.success(`成功删除【${record.name}】`);
              this.st.reload();
            });

            
            // console.log(record, modal, comp)
            // comp.removeRow(record);
          }
        },
      ]
    }
  ];

  constructor(private http: _HttpClient, private modal: ModalHelper, private message: NzMessageService) { }

  ngOnInit() { }

  add() {
    this.modal
      .createStatic(SysUserEditComponent, { i: { id: 0 } })
      .subscribe(() => this.st.reload());
  }

}
